package data_views

import (
	"github.com/labstack/echo/v4"
	"rabbit_love/src/service/client/daos/data_daos"
	"rabbit_love/src/service/client/forms/request/data_forms"
	"rabbit_love/src/service/client/utils"
	"rabbit_love/src/utils/response"
)

func GetDataListApi(ctx echo.Context) error {
	var (
		form data_forms.GetDataRequest
		err  error
	)
	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		loverId int
	)
	loverId = utils.GetLoverId(ctx)
	if loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	var data []map[string]interface{}
	data, err = data_daos.GetDataListDao(loverId, &form)
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}

	return response.RespSuccess(ctx, "查询成功", data)
}

func AddDataListApi(ctx echo.Context) error {
	var (
		form data_forms.AddDataRequest
		err  error
	)
	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	form.Pic, err = ctx.FormFile("pic")

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		userId  int
		loverId int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	if err = data_daos.AddDataListDao(loverId, userId, &form); err != nil {
		return response.RespErr(ctx, "发布失败")
	}

	return response.RespSuccess(ctx, "发布成功", "发布成功")
}

func UpdDataListApi(ctx echo.Context) error {
	var (
		form data_forms.UpdDataRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	form.Pic, err = ctx.FormFile("pic")

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		userId  int
		loverId int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	if err = data_daos.UpDataListDao(loverId, userId, &form); err != nil {
		return response.RespErr(ctx, "更新失败")
	}

	return response.RespSuccess(ctx, "更新成功", "更新成功")
}
